package org.yangspring.samples.mvc.simple;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.FrameworkServlet;
import org.yangspring.samples.mvc.domain.AnimalJoin;
import org.yangspring.samples.mvc.service.AnimalJoinService;
import org.yangspring.samples.mvc.serviceOracle.VdatafileServiceImpl;

import java.util.List;

import static java.lang.System.out;

/**
 * Created by whyang on 2017/06/19.
 */

@Controller
//@RestController
@RequestMapping("/animal")
public class AnimalController {
    private static final Logger logger = LoggerFactory.getLogger(AnimalController.class);

    /*private AnimalJoinService animalService;

    @Autowired
    @Qualifier(value = "animalService")
    public void setAnimalService(AnimalJoinService service){
        this.animalService = service;
    }*/

    @Autowired
//    @Resource(name = "animalService")
    private AnimalJoinService animalService;

    @RequestMapping(value = "/list")
    public String listAnimal(Model model){

        model.addAttribute("animal001", new AnimalJoin());

        List<AnimalJoin> animallls = this.animalService.listAnimal();
        logger.warn("\n\nanimallls Quantity:\t" + animallls.size() + "\n\n");

        model.addAttribute("listAnimal007", animallls);
        return "listAnimal";
    }

    @RequestMapping(value = "/add")
    public String addAnimal(@ModelAttribute("animal001") AnimalJoin animal){

        this.animalService.addAnimal(animal);
        return "addAnimal";
    }

    @RequestMapping("/remove/{id}")
    public String removeAnimal(@PathVariable("id") long id){
        this.animalService.removeAnimal(id);
        return "redirect:/animal";
    }

    @RequestMapping("/edit/{id}")
    public String editAnimal(@PathVariable("id") long id, Model model){
        model.addAttribute("animal", this.animalService.getAnimalById(id));
        model.addAttribute("listAnimal", this.animalService.listAnimal());
        return "animal";
    }


    DispatcherServlet dispSvlt;
    FrameworkServlet frmwkSvlt;

    ContextLoaderListener cntxLdrLsnr;
    ContextLoader cntxLdr;

}
